package com.itheima.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @description:
 * @author: dalao
 * @date: 2020/4/11-16:26
 * @Version: 1.0.0
 */
@Component
public class JedisUtils {

    @Autowired
    private  JedisPool jedisPool;

    public Long del(String key){
        Jedis jedis = null;
        try{
            jedis  = jedisPool.getResource();
            Long del = jedis.del(key);
            return del ;
        }catch (Exception e){
            e.printStackTrace();

        }finally {
            jedis.close();
        }
        return 0L;
    }
    public String get(String key){
        Jedis jedis = null;
        try{
            jedis  = jedisPool.getResource();
            String value = jedis.get(key);
            return value ;
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }finally {
            jedis.close();
        }
    }

    public  String setex(String key, int seconds, String value){
        Jedis jedis = null;
        try{
            jedis  = jedisPool.getResource();
            jedis.setex(key, seconds, value);
            return "";
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }finally {
            jedis.close();
        }

    }
}
